﻿using Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class EActionDataCtr : Bambom.Singleton<EActionDataCtr>
{
    public UnitActionInfo mainData;
    private Data.ActionGroup curGroupData;

    private string curFileName = string.Empty;
    private string savePath = string.Empty;
    protected override void OnInit()
    {
        base.OnInit();
    }

    public List<Data.ActionGroup> GetGroupList()
    {
        return mainData.ActionGroups;
    }
    public void DelateGroup(Data.ActionGroup data)
    {
        mainData.ActionGroups.Remove(data);
    }
    public Data.ActionGroup AddGroupData()
    {
        var data = new Data.ActionGroup();
        mainData.ActionGroups.Add(data);
        return data;
    }
    public void SetCurGroupData(Data.ActionGroup actGroup)
    {
        if (actGroup == null)
        {
            FucTempLog.LogError("设置当前数据为空!");
            return;
        }
        curGroupData = actGroup;
    }
    public Data.ActionGroup CurGroupData()
    {
        return curGroupData;
    }

    public bool OpenTargetData(string path)
    {
        curFileName = Path.GetFileNameWithoutExtension(path);
        savePath = path.Substring(0,path.Length -  Path.GetFileName(path).Length - 1);
        mainData = DataProcessTool.DeserializeData<UnitActionInfo>(path, ParseEnum.ParseXml);
        if (mainData != null)
        {
            curFileName = Path.GetFileNameWithoutExtension(path);
            if (mainData.ActionGroups.Count <= 0)
            {
                mainData.ActionGroups.Add(new Data.ActionGroup());
            }
            curGroupData = mainData.ActionGroups[0];
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool CreateActionData()
    {
        string dataPath = Application.dataPath;
        savePath = ActionEditorInterface.OpenFolderPanel(dataPath);
        if (!string.IsNullOrEmpty(savePath))
        {
            if (savePath.StartsWith(dataPath))
            {              
                mainData = new UnitActionInfo();
                if (mainData.ActionGroups.Count <= 0)
                {
                    mainData.ActionGroups.Add(new Data.ActionGroup());
                }
                curGroupData = mainData.ActionGroups[0];

                var newFileName = "Action_" + mainData.ID;

                curFileName = newFileName;
                return true;
            }
            else
            {
                // ShowNotification(new GUIContent("不能在Assets目录之外!"));
                return false;
            }
        }
        return false;
    }
    public void SaveGroupData()
    {
        if (mainData != null)
        {
            if (!string.IsNullOrEmpty(curFileName))
            {
                if (mainData.ID <= 0)
                {
                    FucTempLog.LogError("动作信息ID 必须填写");
                    return;
                }
                curFileName =  "Action_" + mainData.ID;
                var path = savePath + @"\" + curFileName + ".xml";
                File.Delete(path);
                mainData.BuildActionCache();
                if (DataProcessTool.SerializeData<UnitActionInfo>(path, mainData, ParseEnum.ParseXml))
                {
                    Debug.Log(string.Format("{0} -- 动作文件 <{1}> 成功保存至 ：{2} ！！！", DateTime.Now.ToString("yyyy-mm-dd-hh-mm-ss"), curFileName, path));
                }
            }
            else
            {
                //ShowNotification(new GUIContent("请填写文件名！！！"));
            }
        }

    }
}
